﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.ComponentModel;
using System.Text.RegularExpressions;


    public static class Thuvien
    {
        public static string getConnString()
        {
            string conn = ConfigurationManager.ConnectionStrings["Friesland_Campina_CRM_Server"].ConnectionString.ToString();
            return conn;
        }

        public static DataTable ToDataTable<T>(this IList<T> data)
        {
            PropertyDescriptorCollection props =
                TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            for (int i = 0; i < props.Count; i++)
            {
                PropertyDescriptor prop = props[i];
                table.Columns.Add(prop.Name, prop.PropertyType);
            }
            object[] values = new object[props.Count];
            foreach (T item in data)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = props[i].GetValue(item);
                }
                table.Rows.Add(values);
            }
            return table;
        }

        //Ham chuyen doi chuoi rong
        public static string myConvertNullString(string str)
        {
            string temp = "";
            if (String.IsNullOrEmpty(str) == true)
                return temp;
            return str;
        }

        //Ham chuyen doi chuoi dinh dang sang ngay dd/MM/yyyy
        public static DateTime? myConvertStringToShortDate(string pDatetimeStr)
        {
            pDatetimeStr = pDatetimeStr.Trim();
            if (pDatetimeStr != "")
            {
                IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("en-GB", true);
                DateTime result = DateTime.ParseExact(pDatetimeStr, "dd/MM/yyyy", theCultureInfo);
                return result;
            }
            else
                return null;
        }

        //Ham chuyen doi chuoi dinh dang sang ngay dd/MM/yyyy hh:mm:ss
        public static DateTime? myConvertStringToLongDate(string pDatetimeStr)
        {
            pDatetimeStr = pDatetimeStr.Trim();
            if (pDatetimeStr != "")
            {
                IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("en-GB", true);
                DateTime result = DateTime.ParseExact(pDatetimeStr, "dd/MM/yyyy HH:mm:ss", theCultureInfo);
                return result;
            }
            else
                return null;
        }

        public static IFormatProvider datetimeFormatCustom()
        {
            System.Globalization.CultureInfo tem = new System.Globalization.CultureInfo("en-GB", true);
            tem.DateTimeFormat.FullDateTimePattern = "dd/MM/yyy hh:mm:ss";
            return tem;
        }

        //Ham chuyen doi ngay sang chuoi dinh dang dd/MM/yyyy hh:mm:ss
        public static string myConvertDatetimeToString(DateTime? pDatetime)
        {
            try
            {
                return Convert.ToDateTime(pDatetime).ToString("dd/MM/yyyy HH:mm:ss");
            }
            catch
            {
                return "";
            }
        }

        //Ham chuyen doi ve chuoi khong dau
        public static string ConvertToUnSign(string text)
        {
            for (int i = 33; i < 48; i++)
            {
                text = text.Replace(((char)i).ToString(), "");
            }

            /*for (int i = 58; i < 65; i++)
            {
                text = text.Replace(((char)i).ToString(), "");
            }*/

            //ASCII ":" 58, lau dau ":" trong ca lam viec cua NA
            for (int i = 59; i < 65; i++)
            {
                text = text.Replace(((char)i).ToString(), "");
            }

            for (int i = 91; i < 97; i++)
            {
                text = text.Replace(((char)i).ToString(), "");
            }

            for (int i = 123; i < 127; i++)
            {
                text = text.Replace(((char)i).ToString(), "");
            }

            //text = text.Replace(" ", "-"); //Comment lại để không đưa khoảng trắng thành ký tự -

            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }

        //Ham tinh khoang cach giua 2 toa do
        public static double distanceGPS(double lat1, double lng1, double lat2, double lng2)
        {
            double pi80 = (Math.PI) / 180;
            lat1 *= pi80;
            lng1 *= pi80;
            lat2 *= pi80;
            lng2 *= pi80;

            double r = 6372.797; // mean radius of Earth in km
            double dlat = lat2 - lat1;
            double dlng = lng2 - lng1;
            double a = Math.Sin(dlat / 2) * Math.Sin(dlat / 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(dlng / 2) * Math.Sin(dlng / 2);
            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            double km = r * c;
            return km * 1000;
        }
    }

    

